package com.example.com.baruffini_mele.booking4u.authorization;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHandler extends SQLiteOpenHelper {
	
	// All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "usersManager";
 
    // Users table name
    private static final String TABLE_USERS = "users";
 
    // Users Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_PASSWORD = "password";
   //per utilizzi futuri
   private static final String KEY_COGNOME = "cognome";
   private static final String KEY_NOME = "nome";
   private static final String KEY_COD_FISCALE = "cod_fiscale";
   private static final String KEY_MATRICOLA = "matricola";
    
    
    
    
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
   //Creating Tables
  	@Override
  	public void onCreate(SQLiteDatabase db) {
  	    String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
  	            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_USERNAME + " TEXT,"
  	            + KEY_PASSWORD + " TEXT" + KEY_COGNOME + " TEXT," 
  	            + KEY_NOME + " TEXT," + KEY_COD_FISCALE + " TEXT,"
  	            + KEY_MATRICOLA + " TEXT" + ")";
  	    db.execSQL(CREATE_USERS_TABLE);
  	    
  	    
  	} 
  	
  // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
 
        // Create tables again
        onCreate(db);
    }
    
  //CRUD operations
   // Adding new user
    public void addUser(User user) {
    	SQLiteDatabase db = this.getWritableDatabase();
    	 
        ContentValues values = new ContentValues();
        values.put(KEY_USERNAME, user.getUsername()); // User username
        values.put(KEY_PASSWORD, user.getPassword()); // User password
        //values.put(KEY_COGNOME, user.getCognome().toUpperCase()); // User cognome
        //values.put(KEY_NOME, user.getNome().toUpperCase()); //User nome
        //values.put(KEY_COD_FISCALE, user.getCod_fiscale()); // User codice fiscale
        //values.put(KEY_MATRICOLA, user.getMatricola()); // user matricola
        
             
        // Inserting Row
        db.insert(TABLE_USERS, null, values);
        db.close(); // Closing database connection
    	
    	
    }
    
   // Getting single user by id user
    public User getUser(int id) {
    	SQLiteDatabase db = this.getReadableDatabase();
    	 
        Cursor cursor = db.query(TABLE_USERS, new String[] { KEY_ID,
                KEY_USERNAME, KEY_PASSWORD}, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
     
        User user = new User(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        // return contact
        cursor.close();
        db.close();
        return user;
    }
    
    
    // Getting the unique user
    public User getUniqueUser() {
    	SQLiteDatabase db = this.getReadableDatabase();
    	
    	Cursor cursor = db.query(TABLE_USERS, new String[] { KEY_ID,
                KEY_USERNAME, KEY_PASSWORD }, null,
                null, null, null, null, null);
    	if (cursor != null)
            cursor.moveToFirst();
        User user = new User(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        // return contact
        cursor.close();
        db.close();
        return user;
    }
    
    
    
  
    // Getting users Count (there must be max 1 user)
    public int getUsersCount() {
    	
    	SQLiteDatabase db = this.getReadableDatabase();
    	Cursor dataCount = db.rawQuery("SELECT COUNT(*) FROM "+ TABLE_USERS, null);
    	dataCount.moveToFirst();
    	int count = dataCount.getInt(0);
    	dataCount.close();
    	db.close();
    	return  count;
    	
    }
    
  
    
    // Updating user
    public void updateUser(User user) {
    	SQLiteDatabase db = this.getWritableDatabase();
    	 
        ContentValues values = new ContentValues();
        values.put(KEY_USERNAME, user.getUsername());
        values.put(KEY_PASSWORD, user.getPassword());
        //values.put(KEY_COGNOME, user.getCognome().toUpperCase()); // User cognome
        //values.put(KEY_NOME, user.getNome().toUpperCase()); //User nome
        //values.put(KEY_COD_FISCALE, user.getCod_fiscale()); // User codice fiscale
        //values.put(KEY_MATRICOLA, user.getMatricola()); // user matricola
        
        
     
        // updating row
         db.update(TABLE_USERS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(user.getId()) });
       db.close();
    }
     
    // Deleting  user
    public void deleteUser(User user) {
    	SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_USERS, KEY_ID + " = ?",
                new String[] { String.valueOf(user.getId()) });
        db.close();
    }
    
    // Deleting all  users
    public void deleteAllUsers() {
    	SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_USERS, null,
                null);
        db.close();
    }

}

	
